//
//  SaleBDCell.m
//  SmallEasyLife
//
//  Created by qianfeng on 15/10/24.
//  Copyright © 2015年 GXK. All rights reserved.
//

#import "SaleBDCell.h"
#import "UIImageView+WebCache.h"
#import "Event.h"
#import "Themes.h"
#import "ThemeController.h"

//469
//261

@interface SaleBDCell ()


@property (weak, nonatomic) IBOutlet UILabel *dateTitle;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

//美天的Event图片
@property (weak, nonatomic) IBOutlet UIImageView *eventImageView;

//和美辑 一样的图片
@property (weak, nonatomic) IBOutlet UIImageView *themeImageView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *themeHeightConstraints;


@end

@implementation SaleBDCell

- (void)setModel:(BDNodeModel *)model
{
    
    if (_model != model) {
        _model = model;
        
        Event * eventModel = [model.events firstObject];
        
        self.dateTitle.text = model.date;
        [self.eventImageView sd_setImageWithURL:[eventModel.imgs firstObject]];
        self.titleLabel.text = eventModel.feeltitle;
        self.themeHeightConstraints.constant = 0;
        self.themeImageView.hidden = YES;
        if (model.themes.count != 0 ) {
            
            Themes * themeModel = [model.themes firstObject];
            
            self.themeHeightConstraints.constant = 199;
            self.themeImageView.hidden = NO;
//            NSLog(@"themeModel -> %@",themeModel);
            [self.themeImageView sd_setImageWithURL:[NSURL URLWithString:themeModel.img]];
            
        }
        
        //为图片添加tap手势
        if (!self.eventImageView.gestureRecognizers)
        {
            UITapGestureRecognizer * tapEventGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(eventImageViewTaped:)];
            [self.eventImageView addGestureRecognizer:tapEventGesture];
            self.eventImageView.userInteractionEnabled = YES;
        }
        if (!self.themeImageView.gestureRecognizers) {
            UITapGestureRecognizer * tapThemeGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(themeImageViewTaped:)];
            [self.themeImageView addGestureRecognizer:tapThemeGesture];
            
            self.themeImageView.userInteractionEnabled = YES;
        }
        
    }
    
    [self layoutIfNeeded];
}
//两种跳转到不同storyboard的方式
- (void)eventImageViewTaped:(UIGestureRecognizer *)gesture
{
    Event * eventModel = [self.model.events firstObject];
    
    //storyBoard -》 Storyboard Reference 和直接脱线一样，选中vc选 editor-》refactor to storyboard 组合成一个新的sb
    [self.ownerViewController performSegueWithIdentifier:@"SaleEventSegue" sender:eventModel];

    
}
- (void)themeImageViewTaped:(UIGestureRecognizer *)gesture
{
    Themes * themeModel = [self.model.themes firstObject];
    //加载另一个sb
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"DisPlayShare" bundle:nil];
//    NSLog(@"storyBoard instantiateInitialViewController -> %@",[storyBoard instantiateInitialViewController]);
    ThemeController * VC = [storyBoard instantiateInitialViewController];
//    NSLog(@"themeImageViewTaped:themeModel -> %@",themeModel);
    VC.webViewUrl = themeModel.themeurl;
    
//    NSLog(@"self.ownerViewController.navigationController - > %@",self.ownerViewController.navigationController);
    
    [self.ownerViewController.navigationController pushViewController:VC animated:YES];
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
